package com.example.imtoken.modules.user.create.backups.mnemonic

import android.content.Intent
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ConvertUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityMnemonicWordsBinding
import com.example.imtoken.modules.user.create.backups.mnemonic.confirmation.ConfirmationMnemonicsActivity
import com.xuexiang.xui.adapter.recyclerview.GridDividerItemDecoration

/**
 * 备份助记词
 */
class MnemonicWordsActivity :
    BaseActivity<MnemonicWordsViewModel, ActivityMnemonicWordsBinding>(MnemonicWordsViewModel::class.java) {

    private val mnemonic by lazy {
        intent.getStringExtra("mnemonic")
    }

    private lateinit var adapter: MnemonicWordsAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_mnemonic_words
    }

    override fun init() {
        dataBinding.headView.vLine.visibility = View.INVISIBLE
        initClick()
        initAdapter()
        initObserve()
        viewModel.initWords(mnemonic)
    }

    private fun initAdapter() {
        adapter = MnemonicWordsAdapter(R.layout.item_mnemonic_word)
        dataBinding.rvMnemonic.layoutManager = GridLayoutManager(this, 3)
        dataBinding.rvMnemonic.addItemDecoration(
            GridDividerItemDecoration(
                this,
                3,
                ConvertUtils.sp2px(1f),
                ContextCompat.getColor(this, R.color.viewLine)
            )
        )
        dataBinding.rvMnemonic.adapter = adapter
    }

    private fun initObserve() {
        viewModel.wordsLiveData.observe(this) {
            adapter.setList(it)
        }
    }

    private fun initClick() {
        dataBinding.headView.ivHeadBack.setOnClickListener { this.finish() }
        ClickUtils.applyGlobalDebouncing(dataBinding.tvBackup) {
            val intent = Intent(this, ConfirmationMnemonicsActivity::class.java)
            intent.putExtra("mnemonic", mnemonic)
            ActivityUtils.startActivity(intent)
            this.finish()
        }
    }

}